package com.zhangfb95.crosschain.server.controller;

import com.zhangfb95.crosschain.infra.model.Result;
import com.zhangfb95.crosschain.protocol.exc.ExcChainService;
import com.zhangfb95.crosschain.protocol.exc.request.GetBlockByNumberExcReq;
import com.zhangfb95.crosschain.protocol.exc.request.GetHeightExcReq;
import com.zhangfb95.crosschain.protocol.exc.request.GetTxByIdExcReq;
import com.zhangfb95.crosschain.protocol.exc.request.QueryExcReq;
import com.zhangfb95.crosschain.protocol.exc.request.SendExcReq;
import com.zhangfb95.crosschain.protocol.exc.response.GetBlockByNumberExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.GetHeightExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.GetTxByIdExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.ListChainPathExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.QueryExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.SendExcRes;
import com.zhangfb95.crosschain.protocol.exc.response.TestExcRes;
import com.zhangfb95.crosschain.server.infra.config.properties.WebServerProperties;
import com.zhangfb95.crosschain.server.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangfubing
 * @since 2023/10/18
 */
@RequestMapping("/exc/chain")
@RestController
public class ExcChainController implements ExcChainService {

    @Autowired
    private WebServerProperties webServerProperties;
    @Autowired
    private ChainService chainService;

    @GetMapping("/test")
    @Override
    public Result<TestExcRes> test() {
        TestExcRes res = new TestExcRes()
                .setServerId(webServerProperties.getServerId())
                .setTimestamp(System.currentTimeMillis());
        return Result.ok(res);
    }

    @GetMapping("/listChainPath")
    @Override
    public Result<ListChainPathExcRes> listChainPath() {
        List<String> localChainPaths = chainService.listChainPath();
        ListChainPathExcRes res = new ListChainPathExcRes()
                .setServerId(webServerProperties.getServerId())
                .setChainPaths(localChainPaths);
        return Result.ok(res);
    }

    @PostMapping("/getHeight")
    @Override
    public Result<GetHeightExcRes> getHeight(@RequestBody @Validated GetHeightExcReq req) {
        return Result.ok(chainService.getHeight(req));
    }

    @PostMapping("/getBlockByNumber")
    @Override
    public Result<GetBlockByNumberExcRes> getBlockByNumber(@RequestBody @Validated GetBlockByNumberExcReq req) {
        return Result.ok(chainService.getBlockByNumber(req));
    }

    @PostMapping("/getTxById")
    @Override
    public Result<GetTxByIdExcRes> getTxById(@RequestBody @Validated GetTxByIdExcReq req) {
        return Result.ok(chainService.getTxById(req));
    }

    @PostMapping("/query")
    @Override
    public Result<QueryExcRes> query(@RequestBody @Validated QueryExcReq req) {
        return Result.ok(chainService.query(req));
    }

    @PostMapping("/send")
    @Override
    public Result<SendExcRes> send(@RequestBody @Validated SendExcReq req) {
        return Result.ok(chainService.send(req));
    }
}
